package org.mte.sak.file.filter;

import java.io.File;
import java.io.FilenameFilter;

/**
 * <p>Filtro per la determinazione di una lista
 * di file che rispettono i pattern specificati.</p>
 *
 * <p>Es:</p>
 *
 * <p>Il fileSystem contiene:</p>
 *
 * <ul>
 * 		<li>nsys-config.test</li>
 * 		<li>nsys-config-test.xml</li>
 * 		<li>nsys-config.xml</li>
 * </ul>
 *
 * <p>Si ha: <code>patterns = {"nsys-config.xml", "*.test"}</code></p>
 *
 * <p>I file selezionati saranno:</p>
 * <ul>
 * 	<li>nsys-config.test</li>
 *  <li>nsys-config.xml</li>
 * </ul>
 *
 * <p>Se si ragiona a logica negata si avrà:</p>
 *
 * <ul>
 * 	<li>nsys-config-test.xml</li>
 * </ul>
 *
 * @author Marco Traversari
 * @version $Revision: 1.1 $ $Date: 2011-04-18 08:38:05 $
 */
public class PatternFilter implements FilenameFilter {

    /**
     * patternNames
     */
    private String[] patternNames = null;

    /**
     * patternExts
     */
    private String[] patternExts = null;

    /**
     * isNegativeLogic
     */
    private boolean isNegativeLogic = false;


    /**
     * PatternFilter
     * @param pattern stringa da cercare.
     */
    public PatternFilter(String pattern) {
    	String[] patterns = new String[1];
    	patterns[0] = pattern;
    	init(patterns);
    }

    /**
     * PatternFilter
     * @param patterns
     */
    public PatternFilter(String[] patterns) {
    	init(patterns);
    }

    /**
     * PatternFilter
     * @param patterns
     * @param isNegativeLogic indica se deve ragionare a logica negata
     */
    public PatternFilter(String[] patterns, boolean isNegativeLogic) {
    	this.isNegativeLogic = isNegativeLogic;
    	init(patterns);
    }

    /**
     * Inizializza nomi ed estensioni
     * @param patterns
     */
    private void init(String[] patterns) {
    	patternNames = new String[patterns.length];
    	patternExts = new String[patterns.length];
    	for (int i = 0; i < patterns.length; i++) {
	    	int pos = patterns[i].lastIndexOf(".");
	    	if (pos != -1) {
	    		patternNames[i] = patterns[i].substring(0, pos);
	    		patternExts[i] = patterns[i].substring(pos + 1);
	    	} else {
	    		patternNames[i] = patterns[i];
	    		patternExts[i] = "*";
	    	}
    	}
    }

    /**
     * Controlla se un specificato file deve essere incluso in una
     * lista. La regola implementata � che il suo nome deve
     * contenere la stringa specificata
     *
     * @param   dir � la directory dove viene ricercato il file.
     * @param   name � il nome deil file.
     * @return  <code>true</code> se e solo se il file deve essere incluso,
     * 			<code>false</code> altrimenti.
     */
    public boolean accept(File dir, String name) {
        boolean isFound = false;
        if (dir != null
                && name != null
                && patternNames != null) {
    		String baseName = "";
    		String baseExt = "";
        	int pos = name.lastIndexOf(".");
        	if (pos != -1) {
        		baseName = name.substring(0, pos);
        		baseExt = name.substring(pos + 1);
        	} else {
        		baseName = name;
        		baseExt = "*";
        	}
//        	System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
//        	System.out.println("baseName=" + baseName + "--baseExt=" + baseExt);
    		for (int i = 0; !isFound && i < patternNames.length; i++) {
//            	System.out.println("----------------------------------------------------------------");
//    			System.out.println(i + "--patternNames[i]=" + patternNames[i] + "--patternExts[i]=" + patternExts[i]);
        		if (patternNames[i].equals("*") && patternExts[i].equals("*")) {
//        			System.out.println("pN == * && pE == *");
        			isFound = true;
        		} else if (!patternNames[i].equals("*") && patternExts[i].equals("*")) {
//        			System.out.println("pN != * && pE == *");
        			isFound = patternNames[i].equals(baseName);
        		} else if (patternNames[i].equals("*") && !patternExts[i].equals("*")) {
//        			System.out.println("pN == * && pE != *");
          			isFound = patternExts[i].equals(baseExt);
          		} else if (!patternNames[i].equals("*") && !patternExts[i].equals("*")) {
//        			System.out.println("pN != * && pE != *");
        			isFound = (patternNames[i].equals(baseName) && patternExts[i].equals(baseExt));
        		}
//    			System.out.println("isFound=" + isFound);
//            	System.out.println("----------------------------------------------------------------");
        	}
//        	System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
    	}
        return (isNegativeLogic) ? !isFound : isFound;
    }
}
